<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="50%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> 选择订单 </span>
    </template>
    <div style="color: red; margin-bottom: 10px;font-size: 18px;">
      {{record.message}}
    </div>
    <a-divider />
    <div v-for="(item, key, index) in groupedMap" :key="index" style="padding: 10px 0">
      <div v-if="item.length">
        <div style="margin-bottom: 10px">
          <span style="font-size: 18px; font-weight: bold; margin-right: 20px"
            >{{ index + 1 }}、{{ key }}</span
          >
        </div>
        <div style="margin-bottom: 5px">日期 【 订单 】 供应商 【 数量 】</div>
        <a-checkbox-group v-model:value="item.list">
          <div
            v-for="(item2, index2) in item"
            :key="item2.id"
            style="margin-bottom: 10px; width: 100%"
          >
            <a-checkbox :value="item2">
              {{ item2.parent.createDate }} 【 {{ item2.parent.code }} 】
              {{ item2.parent.basVen?.name || '无' }} 【 {{ item2.syRkQty }} 】
            </a-checkbox>
          </div>
        </a-checkbox-group>
      </div>
      <div v-if="!item.length" style="color: #aaa">
        <div style="margin-bottom: 10px">
          <span style="font-size: 18px; font-weight: bold; margin-right: 20px"
            >{{ index + 1 }}、{{ key }}</span
          >
        </div>
        该图纸暂无订单！
      </div>

      <a-divider />

      <!-- <a-radio-group v-model:value="item.value">
        <div  v-for="(item2, index2) in item" :key="item2.id" style="margin-bottom: 10px;">
          <a-radio :value="item2.id">
            采购单号：{{ item2.parentId }} 【供应商：{{ item2.cusCode || '无' }} 】 数量：{{ item2.iqty }}
          </a-radio>
        </div>
      </a-radio-group> -->
    </div>
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsM8RdRd01M8Rd01FormXzdd',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed, onMounted } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { m8Rd01ScanPicAddRow } from '/@/api/m8/rd/rd01/m8Rd01';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('setting.pricemodel.wecharPriceModelInv');
  const { showMessage } = useMessage();
  const record = ref<WecharPriceModelUse>({} as WecharPriceModelUse);
  const getTitle = computed(() => ({
    icon: 'i-ant-design:book-outlined',
  }));
  let groupedMap = ref({});

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (res) => {
    setDrawerProps({ loading: true });
    record.value = res
    let data = res.data
    for (const key in data) {
      if (data[key].length) {
        data[key].list = [data[key][0]];
      } else {
        data[key].list = [];
      }
    }
    groupedMap.value = data;
    record.value.__t = new Date().getTime();
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    let obj = groupedMap.value;
    let arr = [];
    for (const key in obj) {
      obj[key].list.map((item) => {
        arr.push(item);
      });
    }
    console.log(arr);
    setTimeout(closeDrawer);
    emit('success', arr);
    // const data = await validate();
    // console.log(data);
    // try {
    //   const data = await validate();
    //   setDrawerProps({ confirmLoading: true });
    //   const res = await m8Rd01ScanPicAddRow(data);
    //   // showMessage(res.message);
    //   setTimeout(closeDrawer);
    //   emit('success', res);
    // } catch (error: any) {
    //   if (error && error.errorFields) {
    //     showMessage(t('您填写的信息有误，请根据提示修正。'));
    //   }
    //   console.log('error', error);
    // } finally {
    //   setDrawerProps({ confirmLoading: false });
    // }
  }
</script>
